package com.example.hospital.mapper;

import com.example.hospital.domain.entity.IndicatorResult;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IndicatorResultMapper {
    @Insert("INSERT INTO indicator_result (sample_id, indicator_id, result_value, abnormal_flag, doctor_comment, create_time) VALUES (#{sampleId}, #{indicatorId}, #{resultValue}, #{abnormalFlag}, #{doctorComment}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "resultId")
    int insert(IndicatorResult result);

    @Update("UPDATE indicator_result SET abnormal_flag=#{abnormalFlag}, doctor_comment=#{doctorComment} WHERE result_id=#{resultId}")
    int updateFlagAndComment(@Param("resultId") Long resultId,
                             @Param("abnormalFlag") Boolean abnormalFlag,
                             @Param("doctorComment") String doctorComment);

    // 新增：允许更新结果值，同时更新异常标记与医生备注
    @Update("UPDATE indicator_result SET result_value=#{resultValue}, abnormal_flag=#{abnormalFlag}, doctor_comment=#{doctorComment} WHERE result_id=#{resultId}")
    int updateValueFlagComment(@Param("resultId") Long resultId,
                               @Param("resultValue") String resultValue,
                               @Param("abnormalFlag") Boolean abnormalFlag,
                               @Param("doctorComment") String doctorComment);

    @Select("SELECT * FROM indicator_result WHERE result_id=#{resultId}")
    IndicatorResult findById(@Param("resultId") Long resultId);

    @Select({
            "<script>",
            "SELECT * FROM indicator_result",
            "<where>",
            "  <if test=\"sampleId != null\"> AND sample_id = #{sampleId} </if>",
            "  <if test=\"indicatorId != null\"> AND indicator_id = #{indicatorId} </if>",
            "  <if test=\"abnormalFlag != null\"> AND abnormal_flag = #{abnormalFlag} </if>",
            "</where>",
            "ORDER BY create_time DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<IndicatorResult> list(@Param("sampleId") Long sampleId,
                               @Param("indicatorId") Long indicatorId,
                               @Param("abnormalFlag") Boolean abnormalFlag,
                               @Param("limit") int limit,
                               @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM indicator_result",
            "<where>",
            "  <if test=\"sampleId != null\"> AND sample_id = #{sampleId} </if>",
            "  <if test=\"indicatorId != null\"> AND indicator_id = #{indicatorId} </if>",
            "  <if test=\"abnormalFlag != null\"> AND abnormal_flag = #{abnormalFlag} </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("sampleId") Long sampleId,
               @Param("indicatorId") Long indicatorId,
               @Param("abnormalFlag") Boolean abnormalFlag);

    @Select({
            "<script>",
            "SELECT ir.* FROM indicator_result ir",
            "JOIN sample s ON ir.sample_id = s.sample_id",
            "JOIN examination_order eo ON s.order_id = eo.order_id",
            "<where>",
            "  <if test=\"userId != null\"> AND eo.user_id = #{userId} </if>",
            "  <if test=\"orderId != null\"> AND eo.order_id = #{orderId} </if>",
            "</where>",
            "ORDER BY ir.create_time DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<IndicatorResult> listByUserOrOrder(@Param("userId") Long userId,
                                            @Param("orderId") Long orderId,
                                            @Param("limit") int limit,
                                            @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM indicator_result ir",
            "JOIN sample s ON ir.sample_id = s.sample_id",
            "JOIN examination_order eo ON s.order_id = eo.order_id",
            "<where>",
            "  <if test=\"userId != null\"> AND eo.user_id = #{userId} </if>",
            "  <if test=\"orderId != null\"> AND eo.order_id = #{orderId} </if>",
            "</where>",
            "</script>"
    })
    long countByUserOrOrder(@Param("userId") Long userId,
                            @Param("orderId") Long orderId);
}